/*
 * bsp_rtc.c
 *
 *  Created on: 2023年6月28日
 *      Author: mcealt_DS
 */
#include "bsp_rtc.h"
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_rmu.h"
#include "em_prs.h"
#include "stdio.h"
#include "device.h"

/**************************************************************************//**
 * @brief  Configure BURTC to interrupt every BURTC_IRQ_PERIOD and
 *         wake from EM4
 *****************************************************************************/
void initBURTC(void)
{
  BURTC_Init_TypeDef burtcInit = BURTC_INIT_DEFAULT;

  CMU_ClockSelectSet(cmuClock_EM4GRPACLK, cmuSelect_LFXO);
  CMU_ClockEnable(cmuClock_BURTC, true);
  CMU_ClockEnable(cmuClock_BURAM, false);//暂不使用BURAM

  burtcInit.compare0Top = false; // reset counter when counter reaches compare value
  burtcInit.em4comp = true;     // BURTC compare interrupt wakes from EM4 (causes reset)
  burtcInit.em4overflow = true;

  BURTC_Init(&burtcInit);
  BURTC_IntDisable(_BURTC_IEN_MASK);
  BURTC_IntClear(_BURTC_IF_MASK);
  BURTC_CounterReset();

  NVIC_ClearPendingIRQ(BURTC_IRQn);
  NVIC_EnableIRQ(BURTC_IRQn);

  BURTC_IntEnable(BURTC_IEN_OF);
  BURTC_Enable(true);
}


void initSYSRTC(void)
{
  sl_sysrtc_config_t sysrtc_config = SYSRTC_CONFIG_DEFAULT;
  sl_sysrtc_group_config_t group_config = SYSRTC_GROUP_CONFIG_DEFAULT;
  const sl_sysrtc_group_channel_compare_config_t group_compare_channel_config = SYSRTC_GROUP_CHANNEL_COMPARE_CONFIG_EARLY_WAKEUP;

  CMU_ClockSelectSet(cmuClock_SYSRTCCLK,cmuSelect_LFXO);
  CMU_ClockEnable(cmuClock_SYSRTC, true);

  sl_sysrtc_reset();
  sl_sysrtc_init(&sysrtc_config);

  group_config.compare_channel0_enable = true;
  group_config.compare_channel1_enable = true;
  group_config.p_compare_channel1_config = &group_compare_channel_config;
  sl_sysrtc_init_group(0u, &group_config);

  sl_sysrtc_disable_group_interrupts(0u, _SYSRTC_GRP0_IEN_MASK);
  sl_sysrtc_clear_group_interrupts(0u, _SYSRTC_GRP0_IF_MASK);

  NVIC_ClearPendingIRQ(SYSRTC_APP_IRQn);
  NVIC_EnableIRQ(SYSRTC_APP_IRQn);

  // Initialize PRS to start HFXO for early wakeup
  CMU_ClockEnable(cmuClock_PRS, true);
  PRS_ConnectSignal(1UL, prsTypeAsync, prsSignalSYSRTC0_GRP0OUT1); //ToDo，BURTC使用 chan2 来早唤醒HFXO
  PRS_ConnectConsumer(1UL, prsTypeAsync, prsConsumerHFXO0_OSCREQ);

  sl_sysrtc_enable_group_interrupts(0,SYSRTC_GRP0_IF_OVF); //默认开启sysRTC overflow中断
  sl_sysrtc_enable();
}

void setBURTCCount(uint32_t dwCount)
{
  BURTC->CNT = dwCount; //之后需等待sync同步，等待同步代码，放到setCount外部 0829
}

static void setLfxoCtune(uint32_t ctuneTemp)
{
  uint8_t ctune = 0;
  uint8_t temp = (uint8_t)ctuneTemp;
  int ret = getThisParam(lfxoCtune, &ctune);
  if(ret == 0) {
      int value = (int)(ctune - temp);
      if(value>4 || value<-4) {
          setThisParam(lfxoCtune, &temp);
      }
  }
}

bool setRtcXOCtuneUp()
{
  uint32_t temp32;
  temp32 = CMU_OscillatorTuningGet(cmuOsc_LFXO);
  if(temp32 < RTC_XO_CTUNE_MAX )
  {
      temp32++;
      CMU_OscillatorTuningSet(cmuOsc_LFXO,temp32);
      setLfxoCtune(temp32);
      printf("+%ld",temp32);
      return true;
  }
  return false;
}

bool setRtcXOCtuneDown()
{
  uint32_t temp32;
  temp32 = CMU_OscillatorTuningGet(cmuOsc_LFXO);
  if(temp32 > RTC_XO_CTUNE_MIN )
  {
      temp32--;
      CMU_OscillatorTuningSet(cmuOsc_LFXO,temp32);
      setLfxoCtune(temp32);
      printf("-%ld",temp32);
      return true;
  }
  return false;
}

//void BURTC_IRQHandler(void) //已在softwareMultiTmier.c内重定义
//void SYSRTC_APP_IRQHandler(void) //已在softwareMultiTmier.c内重定义
